'use strict';

const Service = require('egg').Service;

class WeatherService extends Service {
  async query() {
    const { ctx, config } = this;
    const { WeatherConfig } = config;
    const { city, appid, appsecret } = WeatherConfig;

    const result = await ctx.curl(`https://tianqiapi.com/free/week?city=${city}&unescape=1&appid=${appid}&appsecret=${appsecret}`, {
      dataType: 'json',
    });

    const data = result.res.data.data;
    const tomorrowWeather = data[1];
    const { wea, win, tem_day, tem_night } = tomorrowWeather;
    const tomorrowWeatherText = `明天天气${wea},${win},白天温度${tem_day}℃,夜晚温度${tem_night}℃,另外今天晚上我自己吃火锅！！`;

    return tomorrowWeatherText;
  }
}

module.exports = WeatherService;
